/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.seari.bean.UserRole;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryUserRole;
import com.seari.service.UserRolePrivilegeService;
import com.seari.service.UserRoleService;
import com.seari.utils.StringHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("userRole/")
public class UserRoleController
{
    @Autowired
    private UserRoleService userRoleService;
    
    @Autowired
    private UserRolePrivilegeService userRolePrivilegeService;
    
    @RequestMapping("getUserRoleByParams")
    @ResponseBody
    public DataGridBean<UserRole> getUserRoleByParams(HttpServletRequest req)
    {
        DataGridBean<UserRole> grid = new DataGridBean<UserRole>();
        String currPage = req.getParameter("page");
        String pageSize = req.getParameter("limit");
        QueryUserRole qur = new QueryUserRole();
        if(StringUtils.isNotBlank(currPage) && StringUtils.isNotBlank(pageSize))
        {
        	qur.setStart((Integer.parseInt(currPage) - 1) * Integer.parseInt(pageSize));
        	qur.setLimit(Integer.parseInt(pageSize));
        }
        List<UserRole> userRoles = userRoleService.getUserRoleByParams(qur);
        int count = userRoleService.getUserRoleCount();
        grid.setRows(userRoles);
        grid.setTotal(count);
        return grid;
    }
    
    @RequestMapping("addUserRole")
    @ResponseBody
    public MessageBean addUserRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String roleName = req.getParameter("roleName");
        String[] upid = req.getParameterValues("upid[]");//数组传进来时，名称会自动添加[]
        if(StringUtils.isBlank(roleName) || ArrayUtils.isEmpty(upid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        QueryUserRole qur = new QueryUserRole();
        qur.setRoleName(roleName);
        int resultNum = userRoleService.checkUserRoleExist(qur);
        if(resultNum > 0)
        {
            //检查是否已存在此角色名
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("已存在此角色名");
        }else
        {
            UserRole userRole = new UserRole();
            userRole.setRoleName(roleName);
            int resultNum2 = userRoleService.addUserRole(userRole);
            if(resultNum2 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色添加失败");
                return mb;
            }
            int[] privilegeArray = new int[upid.length];
            for(int i = 0 ; i < privilegeArray.length ; i++)
            {
                privilegeArray[i] = Integer.parseInt(upid[i]);
            }
            int resultNum3 = userRolePrivilegeService.addUserRolePrivilege(userRole.getUrid(), privilegeArray);
            if(resultNum3 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色添加失败");
            }else
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("角色添加成功");
            }
        }
        return mb;
    }
    
    @RequestMapping("updateUserRole")
    @ResponseBody
    public MessageBean updateUserRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String roleName = req.getParameter("roleName");
        String urid = req.getParameter("urid");
        String[] upid = req.getParameterValues("upid[]");
        if(StringUtils.isBlank(roleName) || StringUtils.isBlank(urid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        QueryUserRole qur = new QueryUserRole();
        qur.setRoleName(roleName);
        qur.setUrid(Integer.parseInt(urid));
        int resultNum = userRoleService.checkUserRoleExist(qur);
        if(resultNum > 0)
        {
            //检查是否已存在此角色名
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("已存在此角色名");
        }else
        {
            UserRole userRole = new UserRole();
            userRole.setRoleName(roleName);
            userRole.setUrid(Integer.parseInt(urid));
            int resultNum2 = userRoleService.updateUserRole(userRole);
            if(resultNum2 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色修改失败");
                return mb;
            }
            if(ArrayUtils.isNotEmpty(upid))
            {
                int[] privilegeArray = new int[upid.length];
                for(int i = 0 ; i < privilegeArray.length ; i++)
                {
                    privilegeArray[i] = Integer.parseInt(upid[i]);
                }
                int resultNum3 = userRolePrivilegeService.deleteUserRolePrivilege(userRole.getUrid());
                if(resultNum3 < 0)
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                    mb.setMessage("角色修改失败,更新权限失败");
                    return mb;
                }
                int resultNum4 = userRolePrivilegeService.addUserRolePrivilege(userRole.getUrid(), privilegeArray);
                if(resultNum4 < 0)
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                    mb.setMessage("角色修改失败,更新权限失败");
                    return mb;
                }else
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                    mb.setMessage("角色修改成功");
                }
            }
        }
        return mb;
    }
    
    @RequestMapping("deleteUserRole")
    @ResponseBody
    public MessageBean deleteUserRole(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String urid = req.getParameter("urid");
        if(StringUtils.isBlank(urid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else
        {
            int resultNum = userRoleService.deleteUserRole(Integer.parseInt(urid));
            if(resultNum < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("角色删除失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("角色删除成功");
            }
        }
        return mb;
    }
}
